# Test3server.py
import socket
from DesModule import des_encrypt, des_decrypt

tcpserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpserver.bind(("192.168.10.51", 2028))
tcpserver.listen(5)
KEY = "12345678"  # 保证KEY长度为8

while True:
    conn, addr = tcpserver.accept()
    print("Connection from:", addr)
    while True:
        try:
            data = conn.recv(1024)
            if not data:
                break
            decrypted_message = des_decrypt(data.decode("utf-8"), KEY)
            print("Received:", decrypted_message)
            ret_data = input("Please reply: ")
            encrypted_reply = des_encrypt(ret_data, KEY)
            conn.send(encrypted_reply.encode('utf-8'))
        except Exception as e:
            print("Error:", e)
            break
    conn.close()